#include "customdialog.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QBitmap>
#include <QPainter>
#include <QPixmap>
#include <QMouseEvent>


/**
 * @brief CustomDialog::CustomDialog
 * @param parent
 * 语音助手界面
 */


CustomDialog::CustomDialog(QWidget* parent) : QDialog(parent) {
    setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
    // 设置透明度
    setWindowOpacity(0.7);
    setFixedSize(500, 280);
    //设置圆角
    QBitmap bmp(this->size());
    bmp.fill();
    QPainter p(&bmp);
    p.setPen(Qt::NoPen);
    p.setBrush(Qt::black);
    p.drawRoundedRect(bmp.rect(),20,20);//像素为10的圆角
    setMask(bmp);
    timer = new QTimer;
    timer->setInterval(500);
    connect(timer, &QTimer::timeout, this, &CustomDialog::changeImg);
    setupUI();
}

// 将语音转为指令并执行
void CustomDialog::audioToFunction(QString text)
{
    if(text.contains("小雨小雨") || text.contains("小语小语")){
        emit call();
    } else if(text.contains("开始使用")) {
        emit myStart();
    } else if(text.contains("退出程序")) {
        emit myQuit();
    } else if(text.contains("切换黑白主题")) {
        emit changeTheme();
    } else if(text.contains("选择微软")) {
        emit chooseM();
    } else if(text.contains("选择谷歌")) {
        emit chooseG();
    } else if(text.contains("选择python")) {
        emit chooseP();
    } else if(text.contains("选择百度")) {
        emit chooseB();
    } else if(text.contains("选择阿里云")) {
        emit chooseA();
    } else if(text.contains("开启语音提示")) {
        emit startAudio();
    } else if(text.contains("关闭语音提示")) {
        emit stopAudio();
    } else if(text.contains("小号字")) {
        emit smallSizeFont();
    } else if(text.contains("中号字")) {
        emit mediumSizeFont();
    } else if(text.contains("大号字")) {
        emit bigSizeFont();
    } else if(text.contains("记住设置")) {
        emit rememberSettings();
    } else if(text.contains("恢复原设置")) {
        emit recoverSettings();
    } else if(text.contains("打开文本框选")) {
        emit showText();
    } else if(text.contains("关闭文本框选")) {
        emit hideText();
    } else if(text.contains("打开放大镜")) {
        emit magnifier();
    } else if(text.contains("关闭放大镜")) {
        emit dMagnifier();
    } else if(text.contains("调大放大镜倍数")) {
        emit bigMagnifier();
    } else if(text.contains("调小放大镜倍数")) {
        emit smallMagnifier();
    } else if(text.contains("打开麒麟百事通")) {
        emit startAi();
    } else if(text.contains("关闭麒麟百事通")) {
        emit stopAi();
    } else if(text.contains("改变鼠标")) {
        emit changeMouse();
    } else {}
}

// 双击窗口消失
void CustomDialog::mouseDoubleClickEvent(QMouseEvent* event)
{
    if (event->button() == Qt::LeftButton)
    {
        this->close();
        emit quitDialog();
    }
}

CustomDialog::~CustomDialog() {}

//手动实现界面随着鼠标的拖动而移动
void CustomDialog::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        lastMousePosition = event->globalPos() - frameGeometry().topLeft();
        event->accept();
    }
}
void CustomDialog::mouseMoveEvent(QMouseEvent *event)
{
    if (event->buttons() & Qt::LeftButton)
    {
        move(event->globalPos() - lastMousePosition);
        event->accept();
    }
}

void CustomDialog::changeImg()
{
    QPixmap dynamic(":/icons/images/dynamic.png");
    QPixmap dynamic1(":/icons/images/dynamic1.png");
    if(showImage){
        int width = 490;
        int height = 120;
        dynamic = dynamic.scaled(width, height, Qt::KeepAspectRatio);
        label2->setPixmap(dynamic);
        showImage = false;
    } else{
        int width = 490;
        int height = 120;
        dynamic1 = dynamic1.scaled(width, height, Qt::KeepAspectRatio);
        label2->setPixmap(dynamic1);
        showImage = true;
    }
}

// 识别到文字
void CustomDialog::changeToText(QString text)
{
    label2->setVisible(false);
    labelText->setVisible(true);
    labelText->setText(text);
}

// 识别中
void CustomDialog::changeToImg()
{
    label2->setVisible(true);
    labelText->setVisible(false);
}

void CustomDialog::setupUI() {
    // 创建渐变背景
    QLinearGradient gradient(0, 0, 0, height());
    gradient.setColorAt(0, QColor(255, 255, 255));  // 起始颜色
    gradient.setColorAt(1, QColor(255, 225, 255));  // 终止颜色

    QPalette palette;
    palette.setBrush(QPalette::Background, QBrush(gradient));
    setPalette(palette);

    // 创建内容布局
    QVBoxLayout* layout = new QVBoxLayout(this);

    QLabel* label = new QLabel();
    labelText = new QLabel();
    label2 = new QLabel();
    QFont font("Arial", 18);
    labelText->setFont(font);
    QPixmap img(":/icons/images/audio.png");
    QPixmap dynamic(":/icons/images/dynamic.png");
    int width = 110;
    int height = 110;
    int width2 = 490;
    int height2 = 120;
    img = img.scaled(width, height, Qt::KeepAspectRatio);
    dynamic = dynamic.scaled(width2, height2, Qt::KeepAspectRatio);
    label->setPixmap(img);
    label2->setPixmap(dynamic);
    layout->addWidget(label, 0, Qt::AlignTop | Qt::AlignHCenter);
    layout->addWidget(label2, 0, Qt::AlignBottom | Qt::AlignHCenter);
    layout->addWidget(labelText, 0, Qt::AlignHCenter | Qt::AlignHCenter);

    timer->start();
    labelText->setVisible(false);

    // 设置布局
    setLayout(layout);
    setWindowOpacity(0.7);
}
